package com.finals.function;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.finals.function.util.Util;

public class MainActivity extends BaseActivity implements OnClickListener {

	/**
	 * 设置界面
	 */
	View settingView;

	/**
	 * 截屏功能
	 */
	View screenShotView;

	/**
	 * 窗体模式
	 */
	View windowModeView;

	View hideFromTastView;

	TextView accessibilityTextView;
	TextView snapTextView;
	TextView windowModeTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		InitView();
	}

	private void InitView() {
		setTitlePadding();

		settingView = findViewById(R.id.setting);
		settingView.setOnClickListener(this);

		screenShotView = findViewById(R.id.screenshot);
		screenShotView.setOnClickListener(this);

		windowModeView = findViewById(R.id.window_mode);
		windowModeView.setOnClickListener(this);

		hideFromTastView = findViewById(R.id.hide_recent);
		hideFromTastView.setOnClickListener(this);

		accessibilityTextView = (TextView) findViewById(R.id.setting_switch);
		snapTextView = (TextView) findViewById(R.id.snap_switch);
		windowModeTextView = (TextView) findViewById(R.id.windowmode_switch);
	}

	@Override
	public void onClick(View view) {
		if (view.equals(settingView)) {
			Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
			startActivity(intent);
		} else if (view.equals(screenShotView)) {
			mApp.startScreenShot(this, 1);
		} else if (view.equals(hideFromTastView)) {
			mApp.ChangeSone(0);
		}
	}

	@Override
	protected void onResume() {
		RefreshUI();
		super.onResume();
	}

	private void RefreshUI() {
		if (android.os.Build.VERSION.SDK_INT > 19) {
			screenShotView.setVisibility(View.VISIBLE);
		} else {
			screenShotView.setVisibility(View.GONE);
		}
		if (mApp.isSupportScreenShot()) {
			snapTextView.setText("已开启");
		} else {
			snapTextView.setText("关闭");
		}
		if (mApp.isAccessibilityServiceRun()) {
			accessibilityTextView.setText("已开启");
		} else {
			accessibilityTextView.setText("关闭");
		}
		if (mApp.getBaseConfig().getWindowMode() == 0) {
			windowModeTextView.setText("模式一");
		} else if (mApp.getBaseConfig().getWindowMode() == 1) {
			windowModeTextView.setText("模式二");
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1 && resultCode == RESULT_OK) {
			if (data != null) {
				Util.Toast(this, "获取截屏权限成功");
				mApp.InitMediaProjection(data);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
